﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Maticsoft.Common;
using System.Text;
using System.Net;

namespace Lawyer.Web
{
    public partial class file : System.Web.UI.Page
    {
        
        Lawyer.BLL.FileInfo bll = new Lawyer.BLL.FileInfo();
        Lawyer.BLL.ArchiveInfo archiveBll = new Lawyer.BLL.ArchiveInfo();

        public string ArchiveID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                ArchiveID = Request.QueryString["id"].ToString();
            }
            catch
            { }
            if (!Page.IsPostBack)
            {
                //btnDelete.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");
                BindData();
            }
        }

        public void BindData()
        {
            string strWhere = "LawyerID = '" + Session["UserName"].ToString() + "'";
            if (ArchiveID != "")
            {
                strWhere = strWhere + " and ArchiveID = '" + ArchiveID + "'";
            }
            List<Model.FileInfo> list = bll.GetModelList(strWhere);
            
            fileList.DataSource = list;
            fileList.DataBind();
            if (fileList.Rows.Count != 0)
            {
                fileList.HeaderRow.TableSection = TableRowSection.TableHeader;
            }
            else
            {
                btnDelete.Visible = false;
            }
        }

        protected void fileList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            fileList.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bll.DeleteFiles(idlist);
            bll.DeleteList(idlist);
            BindData();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < fileList.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)fileList.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
         
                    if (fileList.DataKeys[i].Value != null)
                    {
                        if (idlist.Length == 0)
                        {
                            idlist = idlist+ "'" + fileList.DataKeys[i].Value.ToString() + "',";
                        }
                        else
                        {
                            idlist = idlist + "'"+fileList.DataKeys[i].Value.ToString()+"',";
                        }
                        
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        protected void fileList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    LinkButton lnkBtn = (LinkButton)e.Row.FindControl("btnDown");
            //    lnkBtn.CommandName = "Download";
            //}
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("fileadd.aspx");
        }

        protected void fileList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //if (e.CommandName == "Download")
            //{
            //    string FileID = e.CommandArgument.ToString();
            //    Model.FileInfo model = bll.GetModel(FileID);
            //    if (model != null && model.FileURL.Length != 0)
            //    {
            //        System.Net.WebClient Client = new System.Net.WebClient();
            //        Client.DownloadFile(Server.MapPath(model.FileURL), model.FileName);
            //        return;
            //    }
            //    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "Download failed!", "file.aspx");
            //}
        }
    }
}
